//-------------------------------------------------------	
var selectedYear = $("#conYear").val();
var selectedMon = $("#conMon").val();
var jackMon = [];
var jackYear = [];

var year = [];
var data;

var d = {
	"year" : selectedYear,
	"mon" : selectedMon
}

$(document)
		.ready(
				function() {
					$(document).load(
							contextPath + "productsales_reports/allYearAndMon",
							function(mse) {
								data = eval('(' + mse + ')');

								for ( var key in data) {
									year.push(key);

								}

								addYear();
								addMon();

							});

					$.ajax({
						url : contextPath + "productsales_reports/ListBody",
						dataType : 'json',
						data : JSON.stringify(d),
						success : function(mes) {
							console.log(mes)
							var list = mes.list

							var monValues = [];
							var yearVales = [];
							var now = 0;

							for (i = 0; i < list.length; i++) {
								monValues.push(list[i].monPercent);
								yearVales.push(list[i].yearPercent);
							}

							var monMax = Math.max.apply(Math, monValues);
							var yearMax = Math.max.apply(Math, yearVales);

							for (i = 0; i < list.length; i++) {

								var jaM = new Array();

								if (list[i].monPercent == monMax) {

									var ob = {
										name : list[i].name,
										y : list[i].monPercent,
										sliced : true,
										selected : true
									}

									jackMon.push(ob);
								} else {
									jaM.push(list[i].name);
									jaM.push(list[i].monPercent);
									jackMon.push(jaM);
								}

								var jaY = new Array();

								if (list[i].yearPercent == yearMax) {

									var ob = {
										name : list[i].name,
										y : list[i].yearPercent,
										sliced : true,
										selected : true
									}
									jackYear.push(ob);

								} else {
									jaY.push(list[i].name);
									jaY.push(list[i].yearPercent);
									jackYear.push(jaY);

								}
								now += list[i].yearPercent;
							}

							if (now < 100) {
								var other = [ 'Others', Number(100 - now) ];

								jackYear.push(other)
							}

						},
						type : "POST",
						async : false,
						contentType : "application/json"
					});

					$('#container')
							.highcharts(
									{
										chart : {
											type : 'pie',
											options3d : {
												enabled : true,
												alpha : 45,
												beta : 0
											}
										},
										title : {
											text : ''
										},
										tooltip : {
											pointFormat : '{series.name}: <b>{point.percentage:.1f}%</b>'
										},
										plotOptions : {
											pie : {
												allowPointSelect : true,
												cursor : 'pointer',
												depth : 35,
												dataLabels : {
													enabled : true,
													format : '{point.name}'
												}
											}
										},
										series : [ {
											type : 'pie',
											name : 'Browser share',
											data : jackMon
										} ]
									});

					$('#container2')
							.highcharts(
									{
										chart : {
											type : 'pie',
											options3d : {
												enabled : true,
												alpha : 45,
												beta : 0
											}
										},
										title : {
											text : ''
										},
										tooltip : {
											pointFormat : '{series.name}: <b>{point.percentage:.1f}%</b>'
										},
										plotOptions : {
											pie : {
												allowPointSelect : true,
												cursor : 'pointer',
												depth : 35,
												dataLabels : {
													enabled : true,
													format : '{point.name}'
												}
											}
										},
										series : [ {
											type : 'pie',
											name : 'Browser share',
											data : jackYear
										} ]
									});
				});
// ------------------------------------------------------------------------------------------
$("#year").blur(function() {
	$('#mon').empty();
	addMon()
})

function addYear() {
	var yl = year.length;
	var time = new Date().getFullYear();
	var conYear = $('#conYear')
	var select = $("#year");
	var yearLen = conYear.val().length;
	if (yl < 13) {
		for (i = 0; i < yl; i++) {

			if (yearLen == 0) {

				if (year[i] == time) {

					var element = $(
							"<option selected='selected'  >" + year[i]
									+ "</option>").attr({
						"value" : year[i],
						"id" : year[i]
					});
				} else {

					var element = $("<option>" + year[i] + "</option>").attr({
						"value" : year[i],
						"id" : year[i]
					});
				}

			} else if (yearLen > 1) {

				if (year[i] == conYear.val()) {
					var element = $(
							"<option selected='selected'  >" + year[i]
									+ "</option>").attr({
						"value" : year[i],
						"id" : year[i]
					});

				} else {
					var element = $("<option>" + year[i] + "</option>").attr({
						"value" : year[i],
						"id" : year[i]
					});
				}
			}

			select.append(element)

		}

	}
}
// ------------------------------------
function addMon() {
	var yearNow = $("#year").find("option:selected").attr("value");
	var conMon = $('#conMon').val();

	var time = "0" + (new Date().getMonth() + 1);
	time = time.substring(time.length - 2)
	var mon = [];
	mon = data[yearNow];

	ml = mon.length;

	var select = $('#mon');
	for (i = 0; i < ml; i++) {
		if (conMon.length > 1) {
			if (mon[i] == conMon) {
				var text = $(
						"<option selected='selected' >" + mon[i] + "</option>")
						.attr({
							"value" : mon[i]
						});
			} else {
				var text = $("<option  >" + mon[i] + "</option>").attr({
					"value" : mon[i]
				});
			}
		} else {
			if (mon[i] == time) {
				var text = $(
						"<option selected='selected' >" + mon[i] + "</option>")
						.attr({
							"value" : mon[i]
						});
			} else {
				var text = $("<option  >" + mon[i] + "</option>").attr({
					"value" : mon[i]
				});
			}
		}
		select.append(text);
	}
}
function aler() {
	alert($("#2014").attr("selected"));
}

function submit() {
	self.location = contextPath + "productsales_reports/page";
}
function search() {
	searchForm.action = contextPath + "productsales_reports/page";
	searchForm.submit();
}